<?php

class Tourtypes extends Controller {

	function Tourtypes()
	{
		parent::Controller();
		$this->load->helper('url');
		$this->load->library('session');
	}
	
	function index()
	{
		$this->load->model('Tourtype_model', '', TRUE);
		
		$data = array();
		
		$data['tours'] = $this->Tourtype_model->get_all_tourtypes();
		
		$this->load->view('tour_types/index', $data);
	}
	
	function show($id)
	{
		$this->load->model('Hostels_model', '', TRUE);
		$this->load->model('Tourtype_model', '', TRUE);
		$this->load->model('Itinerary_model', '', TRUE);
		
		$this->Hostels_model->check_hostel_list();
			
		$data = array();
			
		$data['tour'] = $this->Tourtype_model->get_tourtype($id);
			
		$data['itinerary'] = $this->Itinerary_model->get_itinerary($id);
			
		$this->load->view('tour_types/show', $data);
	}
	
	function add()
	{	
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('tour_name', 'Tour Name', 'required');
		$this->form_validation->set_rules('tour_code', 'Tour Code', 'required');
		$this->form_validation->set_rules('tour_nights', 'Number of nights', 'required|numeric');
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('tour_types/add');
		}
		else
		{
			$this->load->model('Tourtype_model', '', TRUE);
			$id = $this->Tourtype_model->add_tourtype();
			
			$this->session->set_flashdata('msg', 'New Tour Type successfully added');
			redirect(site_url("tourtypes/update/$id"));
		}
	}
	
	function update($id)
	{
		$this->load->helper('hostels');	
		$this->load->model('Hostels_model', '', TRUE);
		$this->load->model('Tourtype_model', '', TRUE);
		$this->load->model('Itinerary_model', '', TRUE);
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('tour_name', 'Tour Name', 'required');
		$this->form_validation->set_rules('tour_code', 'Tour Code', 'required');
		$this->form_validation->set_rules('tour_nights', 'Number of nights', 'required|numeric');
		$this->form_validation->set_rules('default_price', 'Default price', 'numeric');
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->Hostels_model->check_hostel_list();
			
			$data = array();
			
			$data['tour'] = $this->Tourtype_model->get_tourtype($id);
			
			$data['itinerary'] = $this->Itinerary_model->get_itinerary($id);
			
			$this->load->view('tour_types/update', $data);
		}
		else
		{
			$this->Tourtype_model->update($id);
			$this->Itinerary_model->create_or_update($_POST['hostel_names'], $id);
			
			$this->session->set_flashdata('msg', '\'' . $_POST['tour_name'] . '\' successfully updated');
			
			redirect(site_url("tourtypes/update/$id"));
		}
	}
	

}
?>